<template>
  <ma-form-item
    v-if="(typeof props.component.display === 'undefined' || props.component.display === true)"
    :component="props.component"
    :custom-field="props.customField"
  >
    <slot :name="`form-${props.component.dataIndex}`" v-bind="props.component">
      <component
        :is="getComponentName()"
        v-model="value"
        :type="getPickerType()"
        :placeholder="getPlaceholder()"
        :format="props.component.format"
        :value-format="props.component.valueFormat"
        :clearable="props.component.allowClear ?? true"
        :disabled="props.component.disabled"
        :readonly="props.component.readonly"
        :shortcuts="props.component.shortcuts"
        :disabled-date="props.component.disabledDate"
        :disabled-hours="props.component.disabledTime"
        :start-placeholder="props.component.placeholder?.[0]"
        :end-placeholder="props.component.placeholder?.[1]"
        :separator="props.component.separator"
        style="width: 100%"
        @change="handlePickerChangeEvent"
        @calendar-change="handlePickerValueChangeEvent"
        @blur="handlePickerOkEvent"
        @focus="rv('onvVisibleChange', true)"
        @visible-change="rv('onvVisibleChange', $event)"
      />
    </slot>
  </ma-form-item>
</template>

<script setup>
import { ref, inject, onMounted, watch } from 'vue'
import { get, set } from 'lodash'
import MaFormItem from './form-item.vue'
import { runEvent } from '../js/event.js'

const props = defineProps({
  component: Object,
  customField: { type: String, default: undefined }
})

const formModel = inject('formModel')
const getColumnService = inject('getColumnService')
const columns = inject('columns')

const rv = async (ev, value = undefined) => await runEvent(props.component, ev, { formModel, getColumnService, columns }, value)

const index = props.customField ?? props.component.dataIndex
const value = ref(get(formModel.value, index))

watch(() => get(formModel.value, index), vl => value.value = vl)
watch(() => value.value, v => {
  set(formModel.value, index, v)
  if (index.indexOf('.') > -1) delete formModel.value[index]
})

const getComponentName = () => {
  return 'el-date-picker'
}

const getPickerType = () => {
  const typeMap = {
    date: 'date',
    datetime: 'datetime',
    month: 'month',
    year: 'year',
    week: 'week',
    time: 'time',
    range: 'daterange',
    'time-range': 'timerange',
    quarter: 'month', // Element Plus 没有 quarter，可以用 month 模拟或自己处理
  }
  const formType = props.component.formType
  return props.component.range
    ? formType === 'time' ? 'timerange' : 'daterange'
    : typeMap[formType] || 'date'
}

const getPlaceholder = () => {
  if (props.component.range) {
    return props.component.placeholder ?? ['请选择开始时间', '请选择结束时间']
  }
  return props.component.placeholder ?? `请选择${props.component.title}`
}

const handlePickerChangeEvent = (val) => {
  rv('onPickerChange', { value: val })
}

const handlePickerValueChangeEvent = (val) => {
  rv('onPickerValueChange', { value: val })
}

const handlePickerOkEvent = (val) => {
  rv('onOk', { value: val })
}

rv('onCreated')
onMounted(() => rv('onMounted'))
</script>
